home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac C Primer V2 / 3.4 - DLOG / DLOG.c next >
C/C++ Source or Header  |  1991-08-20  |  3KB  |  160 lines

  1. /************************************************************/
  2. /*                                                            */
  3. /*    DLOG Code from Chapter Three of                            */
  4. /*                                                            */
  5. /*        *** The Macintosh Programming Primer ***            */
  6. /*                                                            */
  7. /*    Copyright 1990, Dave Mark                                */
  8. /*                                                            */
  9. /*    This program demonstrates specific Mac programming        */
  10. /*    techniques.                                                */
  11. /*                                                            */
  12. /************************************************************/
  13.  
  14. #define BASE_RES_ID            400
  15. #define NIL_POINTER            0L
  16. #define MOVE_TO_FRONT        (WindowPtr)-1L
  17. #define REMOVE_ALL_EVENTS    0
  18.  
  19. #define OK_ITEM                1
  20. #define CANCEL_ITEM            2
  21. #define TEXT_ITEM            4
  22.  
  23. #define TE_ENTER_KEY        3
  24. #define TE_TAB_CHAR            9
  25. #define TE_CARRIAGE_RETURN    13
  26.  
  27. pascal    Boolean    DLOGFilter();
  28.  
  29. main()
  30. {
  31.     DialogPtr    theDialog;
  32.     Boolean        done;
  33.     int            itemHit, itemType;
  34.     Handle        OKHandle, textHandle;
  35.     Rect        itemRect;
  36.     Str255        theText;
  37.     
  38.     ToolBoxInit();
  39.     
  40.     theDialog = GetNewDialog( BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT );
  41.     GetDItem( theDialog, OK_ITEM, &itemType, &OKHandle, &itemRect );
  42.     GetDItem( theDialog, TEXT_ITEM, &itemType, &textHandle, &itemRect );
  43.     
  44.     CenterDialog( theDialog );
  45.     ShowWindow( theDialog );
  46.     SetPort( theDialog );
  47.     DrawOKButton( theDialog );
  48.     
  49.     done = FALSE;
  50.     while ( ! done )
  51.     {
  52.         GetIText( textHandle, &theText );
  53.         if ( theText[ 0 ] == 0 )
  54.             HiliteControl( OKHandle, 255 );
  55.         else
  56.             HiliteControl( OKHandle, 0 );
  57.         ModalDialog( DLOGFilter, &itemHit );
  58.         done = ( (itemHit == OK_ITEM) || (itemHit == CANCEL_ITEM) );
  59.     }
  60. }
  61.  
  62.  
  63. /*********************************** ToolBoxInit */
  64.  
  65. ToolBoxInit()
  66. {
  67.     InitGraf( &thePort );
  68.     InitFonts();
  69.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
  70.     InitWindows();
  71.     InitMenus();
  72.     TEInit();
  73.     InitDialogs( NIL_POINTER );
  74.     InitCursor();
  75. }
  76.  
  77.  
  78. /***********************************************    DLOGFilter  *****/
  79.  
  80. pascal    Boolean    DLOGFilter( theDialog, e, iPtr )
  81. DialogPtr    theDialog;
  82. EventRecord    *e;
  83. int            *iPtr;
  84. {
  85.     int            itemType;
  86.     Rect        itemRect;
  87.     Handle        item;
  88.     Str255        tempStr;
  89.     char        theChar;
  90.     
  91.     GetDItem( theDialog, TEXT_ITEM, &itemType, &item, &itemRect );
  92.     GetIText( item, &tempStr );
  93.         
  94.     if (e->what == keyDown)
  95.     {
  96.         theChar = (e->message & charCodeMask);
  97.         if ( (theChar == TE_CARRIAGE_RETURN) || (theChar == TE_ENTER_KEY) )
  98.         {
  99.             if ( tempStr[ 0 ] != 0 )
  100.             {
  101.                 *iPtr = OK_ITEM;
  102.                 GetDItem( theDialog, OK_ITEM, &itemType, &item, &itemRect );
  103.                 HiliteControl( item, 1 );
  104.                 return( TRUE );
  105.             }
  106.             else
  107.             {
  108.                 *iPtr = TEXT_ITEM;
  109.                 return( TRUE );
  110.             }
  111.         }
  112.     }
  113.     return( FALSE );
  114. }
  115.  
  116.  
  117. /***********************************************    DrawOKButton  *****/
  118.  
  119. DrawOKButton( theDialog )
  120. DialogPtr    theDialog;
  121. {
  122.     int            itemType;
  123.     Rect        itemRect;
  124.     Handle        item;
  125.     GrafPtr        oldPort;
  126.     
  127.     GetDItem( theDialog, OK_ITEM, &itemType, &item, &itemRect );
  128.     GetPort( &oldPort );
  129.     SetPort( theDialog );
  130.     
  131.     PenSize( 3, 3 );
  132.     InsetRect( &itemRect, -4, -4 );
  133.     FrameRoundRect( &itemRect, 16, 16 );
  134.     PenNormal();
  135.     
  136.     SetPort( oldPort );
  137. }
  138.  
  139.  
  140. /***********************************************    CenterDialog  *****/
  141.  
  142. CenterDialog( theDialog )
  143. DialogPtr    theDialog;
  144. {
  145.     Rect        r;
  146.     int            width, height, sWidth, sHeight, h, v;
  147.     
  148.     r = theDialog->portRect;
  149.     
  150.     width = r.right - r.left;
  151.     height = r.bottom - r.top;
  152.     
  153.     sWidth = screenBits.bounds.right - screenBits.bounds.left;
  154.     sHeight = screenBits.bounds.bottom - screenBits.bounds.top;
  155.     
  156.     h = (sWidth - width) / 2;
  157.     v = (sHeight - height) / 2;
  158.     
  159.     MoveWindow( theDialog, h, v, FALSE );
  160. }